package com.bridgeintelligent.tag.engine.es.base;

import com.bridgeintelligent.tag.engine.es.model.CustomerGroupModel;
import com.bridgeintelligent.tag.engine.es.model.DataSetModel;
import com.bridgeintelligent.tag.engine.es.model.IndexModel;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by JackCheung on 2019/9/24 5:26 PM.
 */
@Slf4j
@Component
public class ESQueryUtils implements ApplicationListener<ContextRefreshedEvent> {

    private EngineESConfig esConfig;
    private static volatile IEngineESHelper iesHelper;

    /**
     * 设置es配置
     *
     * @param esConfig esConfig
     */
    public ESQueryUtils(EngineESConfig esConfig) {
        this.esConfig = esConfig;
    }

    /**
     * 创建索引字段映射
     *
     * @param indexName indexName
     */
    public static void updateIndexMapping(String indexName) {
        iesHelper.updateIndexMapping(indexName);
    }

    /**
     * 查看索引下mapping字段集合
     *
     * @param indexName
     */
    public static Set<String> getIndexMapping(String indexName) {
        return iesHelper.getIndexMapping(indexName);
    }

    /**
     * 批量新增
     *
     * @param indexName indexName
     * @param map       map
     */
    public static void bulkInsert(String indexName, Map<String, Map<String, Object>> map) {
        iesHelper.bulkInsert(indexName, map);
    }

    /**
     * 批量更新
     *
     * @param indexName indexName
     * @param map       map
     */
    public static void bulkUpdate(String indexName, Map<String, Map<String, Object>> map) {
        iesHelper.bulkUpdate(indexName, map);
    }

    /**
     * 数据集查询,根据索引名和查询条件查询数据
     *
     * @param indexName    indexName
     * @param dataSetModel dataSetModel
     * @return SearchResponse
     */
    public static SearchResponse dataSetSearch(String indexName, DataSetModel dataSetModel) {
        return iesHelper.dataSetSearch(indexName, dataSetModel);
    }

    /**
     * 客群分析，根据indexName和customerGroupModel做查询
     *
     * @param indexName          indexName
     * @param customerGroupModel customerGroupModel
     * @return SearchResponse
     */
    public static SearchResponse customerGroup(String indexName, CustomerGroupModel customerGroupModel) {
        return iesHelper.customerGroup(indexName, customerGroupModel);
    }

    /**
     * 聚合分析
     *
     * @param indexModel indexModel
     * @param indexName  indexName
     * @return List<SearchResponse>
     */
    public static List<SearchResponse> indexSearch(IndexModel indexModel, String... indexName) {
        return iesHelper.indexSearch(indexModel, indexName);
    }

    @Override
    public void onApplicationEvent(@SuppressWarnings("NullableProblems") ContextRefreshedEvent contextRefreshedEvent) {
        if (null == iesHelper) {
            initConnect();
        }
    }

    private void initConnect() {
        if (null == iesHelper) {
            synchronized (ESQueryUtils.class) {
                iesHelper = new EngineESHelper(esConfig);
            }
        }
    }

    public void restartConnect() throws IOException {
        synchronized (ESQueryUtils.class) {
            iesHelper.getClient().close();
            iesHelper = new EngineESHelper(esConfig);
            log.error("restart es");
        }
    }

    public static RestHighLevelClient getClient() {
        return iesHelper.getClient();
    }

    public static List<SearchHit[]> scrollSearch(String indexName, BoolQueryBuilder boolQueryBuilder,
                                                 Set<String> fieldsSet) {
        return iesHelper.scrollSearch(indexName, boolQueryBuilder, fieldsSet);
    }
}
